import axios from 'axios';
import { useRouter } from 'vue-router' // 导入 useRouter 钩子
import { ElMessage, ElNotification } from 'element-plus';
import { TokenStore } from './store/user';
import { el } from 'element-plus/es/locales.mjs';



// // 定义接口类型
// export interface ApiResponse<T = any> {
//     code: number;
//     message: string;
//     data: T;
// }

const require = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080/zhongjianyu.cat.com/api',
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json',
    },
});

// 请求拦截器
require.interceptors.request.use(
    (config) => {
        if (!config.headers) {
            // 防止请求头为空
            config.headers = {};
        }
        const store = TokenStore();
        const nowToken = store.accessToken;
        if (nowToken) {
            config.headers.Authorization = nowToken;
        } else {
            ElNotification({
                title: '登录失效',
                message: '请重新登录',
                type: 'warning',
                duration: 2000
            })
            return Promise.reject(new Error('登录失效'));
        }
        console.log('请求拦截器\n', config);
        return config;
    },
    (error) => Promise.reject(error)
);

// 响应拦截器
    require.interceptors.response.use(
        (response) => {
            console.log('响应拦截器\n', response);
            const resp = response.data;
            const code = resp.code;
            const message = resp.msg;
            const data = resp.data;
            if (code !== 200) {
                switch (code) {
                    case 401:
                        // 处理未授权
                        TokenStore().saveToken('');
                        // //弹窗提示登录失效
                        ElNotification({
                            title: '登录失效',
                            message: '请重新登录',
                            type: 'error',
                            duration: 2000
                        })
                        break;
                    default:
                        // 处理其它错误
                        ElNotification({
                            title: '请求失败',
                            message: message,
                            type: 'error',
                            duration: 2000
                        })
                }
            }
            return data||code;
        },
        (error) => {
            const res = error.response;
            if (res) {
                const code = res.status;
                const message = res.data.msg;
                switch (code) {
                    case 401:
                        // 处理未授权
                        TokenStore().saveToken('');
                        // //弹窗提示登录失效
                        ElNotification({
                            title: '登录失效',
                            message: '请重新登录',
                            type: 'error',
                            duration: 2000
                        })
                        break;
                    default:
                        // 处理其它错误
                        ElNotification({
                            title: '请求失败' + code,
                            message: message||error.message,
                            type: 'error',
                            duration: 2000
                        })
                        break;
                }
            } else {
                // 处理未响应
                ElNotification({
                    title: '请求失败',
                    message: error.message,
                    type: 'error',
                    duration: 2000
                })
            }
        }
    );

export default require;